package com.mpyy.handler.inventory;

import com.alibaba.fastjson.JSONArray;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ProSalesUtil {
    public static <T> List<T> decomposeJsonArray(StringBuffer jsonArray, Class clazz) throws Exception {
        //实例化日期格式对象
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //创建需要分解的对象的集合
        List<T> list = new ArrayList<>();
        //获取分解json格式字符串的集合
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) JSONArray.parse(jsonArray.toString());

        Field[] fields = clazz.getDeclaredFields();

        //进行解析
        for (Map<String, Object> map : mapList) {
            //获取当前类反射
            //获取当前类的实例
            T t = (T) clazz.newInstance();

            /*for (Map.Entry<String, Object> stringObjectEntry : map.entrySet()) {
                System.out.println(stringObjectEntry.getKey() + ": " + stringObjectEntry.getValue());
            }
            System.out.println("---------------------------------------------------------");*/

            //获取每一条数据
            for (Map.Entry<String, Object> stringObjectEntry : map.entrySet()) {
                //System.out.println(stringObjectEntry.getKey() + ": " + stringObjectEntry.getValue());
                boolean isFla = true;
                for (Field field : fields) {
                    if (stringObjectEntry.getKey().equals(field.getName())) {
                        isFla = true;
                        break;
                    } else {
                        isFla = false;
                    }
                }
                if (!isFla) {
                    continue;
                }
                // System.out.println(stringObjectEntry.getKey() + ": " + stringObjectEntry.getValue());
                //获取当前属性的数据类型
                Field declaredField = clazz.getDeclaredField(stringObjectEntry.getKey());
                //获取当前类中的属性对应的set方法
                Method method = clazz.getMethod("set" + upwordFirstCase(stringObjectEntry.getKey()), declaredField.getType());

                //判断非空
                if (null != stringObjectEntry.getValue() && !"".equals(stringObjectEntry.getValue())) {

                    /**
                     * 对敏感数据进行处理
                     */
                    if ("Double".equals(declaredField.getType().getSimpleName())) {
                        method.invoke(t, Double.parseDouble(stringObjectEntry.getValue() + ""));
                    } else if ("Date".equals(declaredField.getType().getSimpleName())) {
                        method.invoke(t, dateFormat.parse((String) stringObjectEntry.getValue()));
                    } else if ("Integer".equals(declaredField.getType().getSimpleName())) {
                        method.invoke(t, Integer.parseInt(stringObjectEntry.getValue() + ""));
                    } else {
                        method.invoke(t, stringObjectEntry.getValue());
                    }
                }

            }
            list.add(t);
        }
        return list;
    }

    private static String upwordFirstCase(String s) {
        char first=s.charAt(0);
        if(first>='a'&&first<='z')
            first-=32;
        return first+s.substring(1);
    }
}
